package com.jidu.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jidu.common.core.domain.R;
import com.jidu.common.exception.ServiceException;
import com.jidu.common.utils.DateUtils;
import com.jidu.common.utils.SecurityUtils;
import com.jidu.domain.ClassTimeConfig;
import com.jidu.domain.CourseStudentRel;
import com.jidu.domain.StudentClassTimeRel;
import com.jidu.mapper.StudentClassTimeRelMapper;
import com.jidu.service.IClassTimeConfigService;
import com.jidu.service.ICourseStudentRelService;
import com.jidu.service.IStudentClassTimeRelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 学生上课记录签到Service业务层处理
 * 
 * @author xuliang
 * @date 2024-07-25
 */
@Service
public class StudentClassTimeRelServiceImpl  extends ServiceImpl<StudentClassTimeRelMapper, StudentClassTimeRel> implements IStudentClassTimeRelService
{
    @Autowired
    private StudentClassTimeRelMapper studentClassTimeRelMapper;
    @Autowired
    private ICourseStudentRelService courseStudentRelService;
    @Autowired
    private IClassTimeConfigService configService;

    /**
     * 查询学生上课记录签到
     * 
     * @param id 学生上课记录签到主键
     * @return 学生上课记录签到
     */
    @Override
    public StudentClassTimeRel selectStudentClassTimeRelById(Long id)
    {
        return studentClassTimeRelMapper.selectStudentClassTimeRelById(id);
    }

    /**
     * 查询学生上课记录签到列表
     * 
     * @param studentClassTimeRel 学生上课记录签到
     * @return 学生上课记录签到
     */
    @Override
    public List<StudentClassTimeRel> selectStudentClassTimeRelList(StudentClassTimeRel studentClassTimeRel)
    {
        return studentClassTimeRelMapper.selectStudentClassTimeRelList(studentClassTimeRel);
    }

    /**
     * 新增学生上课记录签到
     * 
     * @param studentClassTimeRel 学生上课记录签到
     * @return 结果
     */
    @Override
    public int insertStudentClassTimeRel(StudentClassTimeRel studentClassTimeRel)
    {
        studentClassTimeRel.setCreateTime(DateUtils.getNowDate());
        return studentClassTimeRelMapper.insertStudentClassTimeRel(studentClassTimeRel);
    }

    /**
     * 修改学生上课记录签到
     * 
     * @param studentClassTimeRel 学生上课记录签到
     * @return 结果
     */
    @Override
    public int updateStudentClassTimeRel(StudentClassTimeRel studentClassTimeRel)
    {
        studentClassTimeRel.setUpdateTime(DateUtils.getNowDate());
        return studentClassTimeRelMapper.updateStudentClassTimeRel(studentClassTimeRel);
    }

    /**
     * 批量删除学生上课记录签到
     * 
     * @param ids 需要删除的学生上课记录签到主键
     * @return 结果
     */
    @Override
    public int deleteStudentClassTimeRelByIds(Long[] ids)
    {
        return studentClassTimeRelMapper.deleteStudentClassTimeRelByIds(ids);
    }

    @Override
    public List<Map<String, Object>> classHourRecord(Long studentId, Long courseStudentId) {
        return studentClassTimeRelMapper.classHourRecord(studentId,courseStudentId);
    }

    @Override
    public List<Map<String, Object>> classTimeList(Long studentId) {
        return studentClassTimeRelMapper.classTimeList(studentId);
    }

    @Transactional
    @Override
    public int
    sign(Long courseStudentId, Long classTimeId,String type) {
        CourseStudentRel one = courseStudentRelService.lambdaQuery()
                .eq(CourseStudentRel::getDelFlag, "0")
                .in(CourseStudentRel::getIsChangeClass,new String[]{"0","2"})
                .in(CourseStudentRel::getStatus,new String[]{"0","1"})
                .eq(CourseStudentRel::getId, courseStudentId).one();
        int i = 0;
        if (one != null){
            Long classId = one.getClassId();
            List<ClassTimeConfig> list = configService.lambdaQuery().eq(ClassTimeConfig::getDelFlag, "0").in(ClassTimeConfig::getStatus,new String[]{"0","1"}).eq(ClassTimeConfig::getClassId, classId).list();
            if (!CollectionUtils.isEmpty(list)){
                int ii = 0;
                for (ClassTimeConfig config : list) {
                    if (config.getId().equals(classTimeId)){
                        ii++;
                    }
                }
                if (ii<=0){
                    throw new ServiceException("暂未查询到该条课程记录!");
                }
            }

            //签到
            ClassTimeConfig one1 = configService.lambdaQuery().eq(ClassTimeConfig::getDelFlag, "0").eq(ClassTimeConfig::getId, classTimeId).one();
            if (one1!=null){
                Long count = this.lambdaQuery().eq(StudentClassTimeRel::getStudentId, one.getStudentId()).eq(StudentClassTimeRel::getDelFlag, "0").eq(StudentClassTimeRel::getStatus, "1")
                        .eq(StudentClassTimeRel::getClassTimeId, one1.getId()).count();
                if (count > 0){
                    throw new ServiceException("该学生该课程该课时已完成签到，请勿重复签到!");
                }
                Long count1 = this.lambdaQuery().eq(StudentClassTimeRel::getStudentId, one.getStudentId()).eq(StudentClassTimeRel::getDelFlag, "0").eq(StudentClassTimeRel::getStatus, "2")
                        .eq(StudentClassTimeRel::getClassTimeId, one1.getId()).count();
                if (count1 > 0){
                    throw new ServiceException("该学生该课程该课时已请假，请勿重复请假!");
                }
                if ("0".equals(type)){
                    int resNum = one.getResNum()-1;
                    if (resNum < 0){
                        throw new ServiceException("您的课时数已用完，请充值!");
                    }
                    one.setResNum(resNum);
                    if ("0".equals(one.getStatus())){
                        one.setStatus("1");
                    }
                    if (resNum == 0){
                        one.setStatus("2");
                    }
                }else if ("1".equals(type)){//请假
                    one.setLeaveTimes(one.getLeaveTimes()+1);
                }
                if ("0".equals(one1.getStatus())){
                    one1.setStatus("1");//已上课
                }
                configService.updateById(one1);
                courseStudentRelService.updateById(one);

                StudentClassTimeRel r = new StudentClassTimeRel();
                r.setStudentId(one.getStudentId());
                r.setDelFlag("0");
                r.setCourseStudentId(courseStudentId);
                r.setClassTimeId(one1.getId());
                r.setCreateTime(new Date());
                r.setUpdateTime(new Date());
                r.setCreateBy(SecurityUtils.getLoginUser().getUserId().toString());
                if ("0".equals(type)){
                    r.setStatus("1");//签到
                    r.setRemark("已签到");
                }else if ("1".equals(type)){
                    r.setStatus("2");//请假
                    r.setRemark("已请假");
                }
                this.save(r);
                i++;
            }
        }
        return i;
    }

    /**
     * 删除学生上课记录签到信息
     * 
     * @param id 学生上课记录签到主键
     * @return 结果
     */
    @Override
    public int deleteStudentClassTimeRelById(Long id)
    {
        return studentClassTimeRelMapper.deleteStudentClassTimeRelById(id);
    }
}
